#include "http.hpp"

HTTPRequestLine::HTTPRequestLine(
    Method m,
    const std::string &path,
    const std::string http_version)
    : method(m),
      path(path)
{
    this->http_version += "HTTP/";
    this->http_version += http_version;
}

std::string HTTPRequestLine::to_string()
{
    std::ostringstream os;
    os << method_str_map[method] << ' ' << path << ' ' << http_version << "\r\n";
    return os.str();
}

HTTPRequestLine::Method HTTPRequestLine::get_method() const
{
    return method;
}

HTTPRequestLine::Method HTTPRequestLine::set_method(HTTPRequestLine::Method m)
{
    Method old = method;
    method = m;
    return old;
}

const std::string &HTTPRequestLine::get_path() const
{
    return path;
}

std::string HTTPRequestLine::set_path(const std::string &new_path)
{
    std::string old_path = path;
    path = new_path;
    return std::move(old_path);
}

HTTPHeader::HTTPHeader(Keys header, const std::string &value) : value(value)
{
    name = header_name[header];
}

std::string HTTPHeader::to_string() const
{
    if (header_name.empty())
    {
        return "";
    }
    std::ostringstream os;
    os << name << ": " << value << "\r\n";
    return os.str();
}

HTTPRequest::HTTPRequest(const HTTPRequestLine &request_line,
                         const std::vector<HTTPHeader> &headers) : request_line(request_line),
                                                                   headers(headers.begin(), headers.end())
{
}

HTTPRequest::HTTPRequest()
{
}

void HTTPRequest::set_method(HTTPRequestLine::Method m)
{
    request_line.set_method(m);
}

std::string HTTPRequest::to_string()
{
    std::string res;
    res += request_line.to_string();
    for (auto &header : headers)
    {
        res += header.to_string();
    }
    res += "\r\n";
    return std::move(res);
}

HTTPRequest::operator const std::string()
{
    return this->to_string();
}

void HTTPRequest::append_header(const HTTPHeader &header)
{
    headers.push_back(header);
}

void HTTPRequest::append_header(HTTPHeader::Keys header, const std::string &value)
{
    headers.emplace_back(header, value);
}

const HTTPRequestLine &HTTPRequest::get_request_line() const
{
    return request_line;
}

std::ostream &operator<<(std::ostream &os, HTTPRequest &request)
{
    return os << request.to_string();
}